package com.yhsoft.buyer.controller;

import com.yhsoft.buyer.controller.request.BuyerAddressInVo;
import com.yhsoft.buyer.controller.response.BuyerAddressOutVo;
import com.yhsoft.buyer.service.IBuyerAddressService;
import com.yhsoft.buyer.service.IBuyerOrderService;
import com.yhsoft.common.request.IdInVo;
import com.yhsoft.common.service.TokenService;
import com.yhsoft.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 地址 前端控制器
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-09-26
 */
@Api(tags = {"G-wx-地址管理接口列表"})
@RestController
@RequestMapping("/buyer/buyerAddress")
public class BuyerAddressController {
    @Resource
    private IBuyerAddressService buyerAddressService;

    @ApiOperation("新增收货地址")
    @PostMapping("/addBuyerAddress")
    public Result<?> addOrUpdateUser(@Valid @RequestBody BuyerAddressInVo inVo){
        buyerAddressService.addAddress(inVo);
        return Result.success("新增收货地址成功！");
    }

    @ApiOperation("修改收货地址")
    @PostMapping("/updateBuyerAddress")
    public Result<?> updateBuyerAddress(@Valid @RequestBody BuyerAddressInVo inVo){
        if(inVo.getId() == null){
            return Result.fail("主键id为空");
        }
        String result = buyerAddressService.updateAddress(inVo);
        if (result.equals("success")) {
            return Result.success("修改收货地址成功！");
        }
        return Result.fail(result);
    }

    @ApiOperation("删除收货地址")
    @DeleteMapping("/delBuyerAddress")
    public Result<?> delBuyerAddress(@NotNull(message = "收货地址id不能为空") @RequestParam Long id){
        buyerAddressService.delBuyerAddress(id);
        return Result.success("删除收货地址成功！");
    }

    @ApiOperation("获得收货地址")
    @GetMapping("/getBuyerAddressList")
    public Result<List<BuyerAddressOutVo>> getBuyerAddressList(){
        List<BuyerAddressOutVo> list =  buyerAddressService.getBuyerAddressList(TokenService.getUserBytoken().getId());
        return Result.success(list);
    }

    @ApiOperation("获取订单地址详情")
    @GetMapping("/detail")
    public Result<BuyerAddressOutVo> getAddress(@RequestParam(value = "orderId") @ApiParam(value = "订单id") String orderId) {
        BuyerAddressOutVo outVo = buyerAddressService.getAddress(orderId);
        return Result.success(outVo);
    }

    @ApiOperation("设置默认收货地址")
    @PostMapping("/setDefalutAddress")
    public Result<?> setDefalutAddress(@Valid @RequestBody IdInVo inVo){
        buyerAddressService.setDefalutAddress(inVo.getId());
        return Result.success("设置默认收货地址成功！");
    }

    @ApiOperation("获取默认收货地址")
    @GetMapping("/getDefalutAddress")
    public Result<BuyerAddressOutVo> getDefalutAddress(){
        BuyerAddressOutVo outVo = buyerAddressService.getDefalutAddress();
        if(outVo == null){
            return Result.success("未获取到默认地址！");
        }
        return Result.success(outVo);
    }
}
